<!DOCTYPE html>
<html>
<head>
    <title>添加药品--中软高科-2015</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap-responsive.css" />
    <link rel="stylesheet" type="text/css" href="../Css/style.css" />
    <script type="text/javascript" src="../Js/jquery.js"></script>
    <script type="text/javascript" src="../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../Js/ckform.js"></script>
    <script type="text/javascript" src="../Js/common.js"></script>
    <script type="text/javascript" src="../Js/ckeditor/ckeditor.js"></script>
    <script type="text/javascript" src="../Js/webUtil.js"></script>
 

    <style type="text/css">
        body {
            padding-bottom: 40px;
        }
        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
    <script type="text/javascript">
    $(function () {
        let url = document.location.toString();
        let index = url.GetValue("index");
        if (index == null){
            index =1;
        }
		$('#backid').click(function(){
				window.location.href="index.html?index="+index;
		 });
    });
    </script>
</head>
<body>
<form id="drugForm" class="definewidth m20">
<table class="table table-bordered table-hover definewidth m10">
    <tr>
        <td width="10%" class="tableleft">药品编号</td>
        <td><input type="text" id="id" name="id" value=""/></td>
    </tr>
    <tr>
        <td width="10%" class="tableleft">图片</td>
        <td><input type="file" name="url" value=""/></td>
    </tr>
    <tr>
        <td width="10%" class="tableleft">进价</td>
        <td><input type="text" name="inPrice" value="" id="inPrice"/></td>
    </tr>
    <tr>
        <td width="10%" class="tableleft">售价</td>
        <td><input type="text" name="outPrice" value="" id="outPrice"/></td>
    </tr>
    <tr>
        <td width="10%" class="tableleft">药品名称</td>
        <td><input type="text" name="name" value="" id="name"/></td>
    </tr>
    <tr>
        <td width="10%" class="tableleft">药品类型</td>
        <td>
            <input type="radio" name="type" value="处方药" checked/>处方药&nbsp;&nbsp;&nbsp;
            <input type="radio" name="type" value="非处方药"/>非处方药
            <input type="radio" name="type" value="中药"/>中药&nbsp;&nbsp;&nbsp;
            <input type="radio" name="type" value="西药"/>西药
        </td>
    </tr>
    <tr>
        <td width="10%" class="tableleft">简单描述</td>
        <td><input type="text" name="simDesc" id="simDesc" value=""/></td>
    </tr>

    <tr>
        <td width="10%" class="tableleft">保质期</td>
        <td><input type="text" name="duration" id="duration" value="36"/>月</td>
    </tr>
    <tr>
        <td width="10%" class="tableleft">详细描述</td>
        <td><textarea name="detaDesc" id="detaDesc"></textarea></td>
    </tr>
    <tr>
        <td width="10%" class="tableleft">生产厂商</td>
        <td><textarea name="factory" id="factory"></textarea></td>
    </tr>
    <tr>
        <td width="10%" class="tableleft">服用说明</td>
        <td><input type="text" name="direction" value="" id="direction"/></td>
    </tr>
   
	<tr>
        <td width="10%" class="tableleft">备注</td>
        <td><textarea name="remark" id="remark"></textarea></td>
	</tr>
    <tr>
        <td colspan="2">
			<center>
				<button id="insert" class="btn btn-primary" type="button">保存</button> &nbsp;&nbsp;
                <button type="button" class="btn btn-success" name="backid" id="backid">返回列表</button>
			</center>
		</td>
    </tr>
</table>
</form>
</body>
<script>
    $(function(){
        let url = document.location.toString();
        let state = url.GetValue("state");
        let id = url.GetValue("id");
        if (state == 1){//当state等于1时界面是修改界面
            loadData(id);
            $("#insert").click(function () {
                update();
            });
        }else if (state==0){//当state=0时界面是新增界面
            $("#insert").click(function () {
                insert();
            });
        }
    });
    function update() {
        let id = $("#id").val();
        if (! id.length>0){
            alert("请输入编号！");
            return;
        }
        if (isNaN(id)){
            alert("编号为数字！");
            return;
        }
        let url  = document.location.toString();
        let index = url.GetValue("index");
        if (index == null){
            index = 1;
        }
        $.ajax({
            type:"POST",
            url:"/drug",
            data:$("#drugForm").serialize()+"&_method=PUT",
            dataType:"json",
            success: function (vo) {
                alert(vo.msg);
                window.location.href="index.html?index="+index;
            }
        });
    }
    function loadData(id){
        $.ajax({
            type:"GET",
            url:"/drug/"+id,
            data:"",
            dataType:"json",
            success: function(vo){
                if (vo.code ==200){
                    let obj = vo.obj;
                    let type = obj.type;
                    $("#id").val(obj.id);
                    $("#id").attr("disabled","disabled");
                    $("#inPrice").val(obj.inPrice);
                    $("#outPrice").val(obj.outPrice);
                    $("#name").val(obj.name);
                    $(":radio[name='type'][value='"+type+"']").attr("checked","checked");

                    $("#simDesc").val(obj.simDesc);
                    $("#duration").val(obj.duration);
                    $("#detaDesc").val(obj.detaDesc);
                    $("#factory").val(obj.factory);
                    $("#direction").val(obj.direction);
                    $("#remark").val(obj.remark);
                }else {
                    alert(vo.msg);
                }
            }
        });
    }

    function insert(){
        let id = $("#id").val();
        if (! id.length>0){
            alert("请输入编号！");
            return;
        }
        if (isNaN(id)){
            alert("编号为数字！");
            return;
        }
        let url  = document.location.toString();
        let index = url.GetValue("index");
        if (index == null){
            index =1;
        }
        $.ajax({
            type:"POST",
            url:"/drug",
            data:$("#drugForm").serialize(),
            dataType:"json",
            success: function (vo) {
                alert(vo.msg);
                window.location.href="index.html?index="+index;
            }
        });
    }
</script>
</html>